package com.dongdongshop.mq;

import com.dongdongshop.util.SendMsmUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Component
@RocketMQMessageListener(consumerGroup = "my-consumer",topic = "cctv")
public class ConsumeLinster implements RocketMQListener<String> {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(String phone) {
        System.out.println("消息消费成功{}{}" + phone);
        //生成验证码
        String str = "";
        Random random = new Random();
        for (int i=0;i<6;i++) {
            str += random.nextInt(10);
        }
        //将电话号码和验证码存入Redis
        String key = "codes";
        String feild =  phone;
        //将结果放入Redis,注意实体类必须序列化
        redisTemplate.boundHashOps(key).put(feild,str);
        //设置有效时间。因为在springboot里村Redis数据时会将数据的key自动帮我们序列化。无法在可视化工具里删除
        redisTemplate.boundHashOps(key).expire(5, TimeUnit.MINUTES);
        //发送短信
        SendMsmUtil.sendMessage(phone,str);

    }
}

